#include "main/utils.h"
using namespace std;

int longestSubArray(const vector<int> &vec) {
  int max_len = 0;
  int sum = 0;
  map<int, int> help_map;
  help_map[0] = -1;
  for (int i = 0; i < vec.size(); ++i) {
    sum += (vec[i] == 0 ? -1 : 1);
    if (help_map.find(sum) != help_map.end()) {
      int cur_len = i - help_map[sum];
      if (cur_len > max_len) {
        max_len = cur_len;
      }
    } else {
      help_map[sum] = i;
    }
  }
  return max_len;
}

int main() {
  vector<int> vec = {0, 1, 0, 1, 0, 0, 0, 1, 1, 1};
  int ret = longestSubArray(vec);
  cout << "The longest sub array is: " << ret << endl;

  return 0;
}
